(function (G) {
    G.component({
        options: {
            multipleSlots: true
        },
        props: {
            name: {
                type: String,
                default: G.guid()
            },
            index: {
                type: Number
            },
            disabled: {
                type: Boolean,
                default: true
            },
            value: {
                type: String,
                default: {},
                observer: function (newValue, oldValue) {
                    this.filter(newValue);
                }
            },
            item: {
                type: Object,
                default: {},
            },
            attr: {
                type: Object,
                default: {}
            }
        },
        data: function () {
            return {
                dialogVisible: false,
                activeName: 'all',
                value1: {},
                tableData: [],
                dataLoaded: false,
                users: [],
                userObj: {},
                groupValue: '',
                groups: [],
                unitValue: '1641331705845518336',
                units: [],
                nameValue: '',
                multipleSelection: [],
                selectIndex:0,
                showValue: '请选择',
                max: 1,
                userInfo: {}
            }
        },
        mounted: function () {
            let attr = this.getData('attr')
            this.setData({
                max:attr.max || 1,
                userInfo:G.Storage.get('userInfo')
            })
            this.filter(this.getData('value'))
        },
        methods: {
            close(){
                this.setData({
                    dialogVisible:false
                })
            },
            filter(value) {
                let attr = this.getData('attr')
                let max = attr.max || 1
                let item = this.getData('item')
                if (max == 1) {
                    if (value == this.data.multipleSelection) return
                    this.setData({
                        multipleSelection:value,
                        showValue:item ? item[attr.skey || 'name'] : (attr.placeholder || '请选择')
                    })
                } else {
                    if (value == JSON.stringify(this.data.multipleSelection)) return
                    if(!value && this.data.multipleSelection && !this.data.multipleSelection.length) return
                    let multipleSelection = G.string(value).parse([])
                    this.setData({
                        multipleSelection,
                        showValue:'选中' + multipleSelection.length + '人'
                    })
                }
            },
            open(value) {
                if (value && typeof value != 'object') {
                    this.filter(value)
                    this.setData({
                        dialogVisible:true
                    })
                } else {
                    this.setData({
                        // multipleSelection:[],
                        dialogVisible:true
                    })
                }
                if (!this.data.groups.length) {
                    G.get('/api/model/group').then(groups => {
                        // this.groups = groups
                        // this.groupValue = groups[0].id
                        G.get('/api/model/hw_res_enterprise').then(units => {
                            this.setData({
                                groups,
                                groupValue:groups[0].id,
                                units
                            })
                            this.getList()
                        })
                    })
                } else {
                    this.getList()
                }
            },
            searchName(event) {
                let nameValue = event.detail.value
                let tableData = [],reg = new RegExp(nameValue)
                for (let i = 0; i < this.data.users.length; i++) {
                    if (reg.test(this.data.users[i].name) || reg.test(this.data.users[i].enterpriceName)) tableData.push(this.data.users[i])
                }
                this.setData({
                    nameValue,
                    tableData
                })
            },
            selectValue(event){
                const tableData = this.data.tableData
                const index = event.currentTarget.dataset.index
                tableData[this.data.selectIndex].checked = false
                tableData[index].checked = !tableData[index].checked
                if(this.data.max == 1) {
                    this.setData({
                        tableData,
                        showValue:tableData[index].name,
                        selectIndex:index,
                        multipleSelection:tableData[index].value
                    })
                }else{
                    let multipleSelection = [],showValue = []
                    for(let i = 0; i < this.data.users.length; i ++){
                        if(this.data.users[i].checked) {
                            multipleSelection.push({
                                name:this.data.users[i].name,
                                value:this.data.users[i].value
                            })
                            showValue.push(this.data.users[i].name)
                        }
                    }
                    this.setData({
                        tableData,
                        showValue:showValue.join(','),
                        multipleSelection
                    })
                }
            },
            getList(event) {
                if(event && event.currentTarget){
                    this.setData({
                        activeName:event.currentTarget.dataset.name,
                        nameValue:''
                    })
                }else{
                    this.setData({
                        nameValue:''
                    })
                }
                let query = {}
                let multipleSelection = {}
                if(this.data.max == 1){
                    multipleSelection[this.data.multipleSelection] = true
                }else{
                    for (let i = 0; i < this.data.multipleSelection.length; i++) {
                        multipleSelection[this.data.multipleSelection[i].value] = true
                    }
                }
                if (this.data.activeName == 'all') {
                    query.status = 1
                    query.sql = 'user_unit'
                    G.get('/api/model/user', query).then(users => {
                        let _users = [],userObj = this.data.userObj
                        for (let i = 0; i < users.length; i++) {
                            let dept = users[i].dept ? users[i].dept : ''
                            dept = dept.replace(new RegExp(users[i].enterpriceName + '/?','g'),'')
                            if(dept) dept = '\r\n' + dept
                            let user = {
                                value: users[i].id,
                                name: users[i].name,
                                unitId: users[i].userUnitId,
                                enterpriceName:users[i].enterpriceName + dept,
                                checked:!!multipleSelection[users[i].id]
                            }
                            userObj[user.value] = user
                            _users.push(user)
                        }
                        this.setData({
                            userObj,
                            dataLoaded:true,
                            tableData:_users,
                            users:_users
                        })
                    })
                } else if (this.data.activeName == 'group') {
                    if (this.data.groupValue) query.groupId = this.data.groupValue
                    G.get('/api/model/group_user?sql=userselect_group_user', query).then(users => {
                        let _users = [],userObj = this.data.userObj
                        for (let i = 0; i < users.length; i++) {
                            let user = {
                                value: users[i].userId,
                                name: users[i].name,
                                unitId: users[i].userUnitId,
                                checked:!!multipleSelection[users[i].userId]
                            }
                            userObj[user.value] = user
                            _users.push(user)
                        }
                        this.setData({
                            userObj,
                            dataLoaded:true,
                            tableData:_users,
                            users:_users
                        })
                    })
                } else if (this.data.activeName == 'unit') {
                    if (this.data.unitValue) query.enterpriceId = this.data.unitValue
                    query.status = 1
                    query.sql = 'user_unit'
                    G.get('/api/model/user', query).then(users => {
                        let _users = [],userObj = this.data.userObj
                        for (let i = 0; i < users.length; i++) {
                            let user = {
                                value: users[i].id,
                                name: users[i].name,
                                unitId: users[i].unitId,
                                enterpriceName:users[i].enterpriceName,
                                checked:!!multipleSelection[users[i].id]
                            }
                            userObj[user.value] = user
                            _users.push(user)
                        }
                        this.setData({
                            userObj,
                            dataLoaded:true,
                            tableData:_users,
                            users:_users
                        })
                    })
                }
            },
            sizeChange(pageSize) {
                let params = this.params
                params.pageSize = pageSize
                params.pageNum = 1
                this.setData({
                    params
                })
                this.getList()
            },
            currentChange(pageNum) {
                let params = this.params
                params.pageNum = pageNum
                this.setData({
                    params
                })
                this.getList()
            },
            clear(event){
                this.setData({
                    multipleSelection:[],
                    showValue:''
                })
                this.change(event)
            },
            close(){
                this.setData({
                    dialogVisible:false
                })
            },
            change(event) {
                this.setData({
                    dialogVisible:false
                })
                if (this.data.max == 1) {
                    this.triggerEvent('change', {
                        index: this.getData('index'),
                        id: this.getData('name'),
                        event,
                        value: this.data.multipleSelection
                    })
                    this.triggerEvent('input', {
                        index: this.getData('index'),
                        id: this.getData('name'),
                        event,
                        value: this.data.multipleSelection
                    })
                } else {
                    this.triggerEvent('change', {
                        index: this.getData('index'),
                        id: this.getData('name'),
                        event,
                        value: this.data.multipleSelection.length ? JSON.stringify(this.data.multipleSelection) : ''
                    })
                    this.triggerEvent('input', {
                        index: this.getData('index'),
                        id: this.getData('name'),
                        event,
                        value: this.data.multipleSelection.length ? JSON.stringify(this.data.multipleSelection) : ''
                    })
                }
            }
        }
    })
})(wx.$)